package com.sgg.algorithm.search;

public class InsertValueSearch {
    public static void main(String[] args) {
        int[] arr = new int[100];
        for (int i = 0; i < 100; i++) {
            arr[i] = i + 1;
        }
        System.out.println(search(arr, 1111));

    }

    public static int search(int[] arr, int value) {
        return search(arr, 0, arr.length - 1, value);
    }


    public static int search(int[] arr, int left, int right, int value) {
        //为了防止mid越界，所以增加后面2个或的判断
        if (left > right || value < arr[left] || value > arr[right]) {
            return -1;
        }
//        int mid = (left + right) / 2;
        int mid = left + (value - arr[left]) * (right - left) / (arr[right] - arr[left]);
        System.out.println(mid);
        if (arr[mid] > value) {
            return search(arr, left, mid - 1, value);
        } else if (arr[mid] < value) {
            return search(arr, mid + 1, right, value);
        } else {
            return mid;
        }
    }
}
